package com.yunmang.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.alertview.AlertView;
import com.bigkoo.alertview.OnItemClickListener;
import com.bigkoo.pickerview.TimePickerView;
import com.bigkoo.pickerview.listener.CustomListener;
import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;
import com.yunmang.R;
import com.yunmang.adapter.BlackBordAdapter;
import com.yunmang.entity.MyItemInfoBean;
import com.yunmang.utils.DateUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/6/21.
 */
public class BlackBordFragment extends BaseFragment implements OnItemClickListener {
    @InjectView(R.id.gridview)
    GridView gridview;
    @InjectView(R.id.tv_today)
    TextView tvToday;
    @InjectView(R.id.tv_yesterday)
    TextView tvYesterday;
    @InjectView(R.id.tv_week)
    TextView tvWeek;
    @InjectView(R.id.tv_month)
    TextView tvMonth;
    @InjectView(R.id.tv_auto)
    TextView tvAuto;
    @InjectView(R.id.springView)
    SpringView springView;
    private BlackBordAdapter adapter;
    private List<MyItemInfoBean> list;
    private String start,end;
    private TimePickerView pvCustomTime,pvendTime;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_blackbord;
    }

    @Override
    public void initView(View view) {
        ButterKnife.inject(this, view);
        tvToday.setEnabled(true);
        springView.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                springView.onFinishFreshAndLoad();
            }
            @Override
            public void onLoadmore() {
                springView.onFinishFreshAndLoad();
            }
        });
        springView.setHeader(new DefaultHeader(getActivity()));
        springView.setFooter(new DefaultFooter(getActivity()));
        springView.setType(SpringView.Type.FOLLOW);
        initCustomTimePicker();
    }

    private void requestData(){

    }

    @Override
    public void initData() {
        start= DateUtil.date2string(new Date(),DateUtil.YYYY_MM_DD);
        end= DateUtil.date2string(new Date(),DateUtil.YYYY_MM_DD);

        list = new ArrayList<>();
        list.add(new MyItemInfoBean(R.mipmap.icon_login_mima, "运费额", "0.0"+"元"));
        list.add(new MyItemInfoBean(R.mipmap.icon_login_name, "货款额",  "0.0"+"元"));
        list.add(new MyItemInfoBean(R.mipmap.icon_sy_dh, "账户余额",  "0.0"+"元"));
        list.add(new MyItemInfoBean(R.mipmap.icon_sy_dh, "出港数", "0"+"笔"));
        list.add(new MyItemInfoBean(R.mipmap.icon_sy_dh, "到港数", "0"+"笔"));
        list.add(new MyItemInfoBean(R.mipmap.icon_sy_dh, "作废数","0"+"笔"));
        list.add(new MyItemInfoBean(R.mipmap.icon_sy_dh, "破损率","0"+"%"));
        list.add(new MyItemInfoBean(R.mipmap.icon_sy_dh, "出库率", "0"+"%"));
        list.add(new MyItemInfoBean(R.mipmap.icon_sy_dh, "发车数", "0"+"笔"));
        gridview.setAdapter(new BlackBordAdapter(getActivity(), list));
        showAlertDialog("暂未开放，敬请期待");
    }

    /**
     * 弹出对话框
     *
     * @param string
     */
    private void showAlertDialog(String string) {
        new AlertView("提示", string, null, new String[]{"确定"}, null, getActivity(), AlertView.Style.Alert, this).show();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    @Override
    public void onItemClick(Object o, int position) {

    }

    @OnClick({R.id.tv_auto,R.id.tv_today, R.id.tv_yesterday, R.id.tv_week, R.id.tv_month})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_auto:
                tvAuto.setTextColor(getResources().getColor(R.color.white));
                tvYesterday.setTextColor(getResources().getColor(R.color.black0));
                tvToday.setTextColor(getResources().getColor(R.color.black0));
                tvWeek.setTextColor(getResources().getColor(R.color.black0));
                tvMonth.setTextColor(getResources().getColor(R.color.black0));
                tvToday.setBackgroundResource(R.drawable.bg_edit);
                tvYesterday.setBackgroundResource(R.drawable.bg_edit);
                tvWeek.setBackgroundResource(R.drawable.bg_edit);
                tvMonth.setBackgroundResource(R.drawable.bg_edit);
                tvAuto.setBackgroundResource(R.drawable.bg_blackbord_press);
                break;
            case R.id.tv_today:
                start= DateUtil.date2string(new Date(),DateUtil.YYYY_MM_DD);
                end= DateUtil.date2string(new Date(),DateUtil.YYYY_MM_DD);
                tvToday.setTextColor(getResources().getColor(R.color.white));
                tvYesterday.setTextColor(getResources().getColor(R.color.black0));
                tvWeek.setTextColor(getResources().getColor(R.color.black0));
                tvMonth.setTextColor(getResources().getColor(R.color.black0));
                tvAuto.setTextColor(getResources().getColor(R.color.black0));
                tvYesterday.setBackgroundResource(R.drawable.bg_edit);
                tvMonth.setBackgroundResource(R.drawable.bg_edit);
                tvWeek.setBackgroundResource(R.drawable.bg_edit);
                tvAuto.setBackgroundResource(R.drawable.bg_edit);
                tvToday.setBackgroundResource(R.drawable.bg_blackbord_press);
                break;
            case R.id.tv_yesterday:
                start=DateUtil.getYesterday();
                end=DateUtil.date2string(new Date(),DateUtil.YYYY_MM_DD);
                start= DateUtil.date2string(new Date(),DateUtil.YYYY_MM_DD);
                end= DateUtil.date2string(new Date(),DateUtil.YYYY_MM_DD);
                tvYesterday.setTextColor(getResources().getColor(R.color.white));
                tvToday.setTextColor(getResources().getColor(R.color.black0));
                tvWeek.setTextColor(getResources().getColor(R.color.black0));
                tvMonth.setTextColor(getResources().getColor(R.color.black0));
                tvAuto.setTextColor(getResources().getColor(R.color.black0));
                tvToday.setBackgroundResource(R.drawable.bg_edit);
                tvMonth.setBackgroundResource(R.drawable.bg_edit);
                tvWeek.setBackgroundResource(R.drawable.bg_edit);
                tvAuto.setBackgroundResource(R.drawable.bg_edit);
                tvYesterday.setBackgroundResource(R.drawable.bg_blackbord_press);
                break;
            case R.id.tv_week:
                start=DateUtil.getThisWeek();
                end=DateUtil.date2string(new Date(),DateUtil.YYYY_MM_DD);
                tvWeek.setTextColor(getResources().getColor(R.color.white));
                tvYesterday.setTextColor(getResources().getColor(R.color.black0));
                tvToday.setTextColor(getResources().getColor(R.color.black0));
                tvMonth.setTextColor(getResources().getColor(R.color.black0));
                tvAuto.setTextColor(getResources().getColor(R.color.black0));
                tvToday.setBackgroundResource(R.drawable.bg_edit);
                tvYesterday.setBackgroundResource(R.drawable.bg_edit);
                tvMonth.setBackgroundResource(R.drawable.bg_edit);
                tvAuto.setBackgroundResource(R.drawable.bg_edit);
                tvWeek.setBackgroundResource(R.drawable.bg_blackbord_press);
                break;
            case R.id.tv_month:
                start=DateUtil.getThisMonth();
                end=DateUtil.date2string(new Date(),DateUtil.YYYY_MM_DD);
                tvMonth.setTextColor(getResources().getColor(R.color.white));
                tvYesterday.setTextColor(getResources().getColor(R.color.black0));
                tvToday.setTextColor(getResources().getColor(R.color.black0));
                tvWeek.setTextColor(getResources().getColor(R.color.black0));
                tvAuto.setTextColor(getResources().getColor(R.color.black0));
                tvToday.setBackgroundResource(R.drawable.bg_edit);
                tvYesterday.setBackgroundResource(R.drawable.bg_edit);
                tvWeek.setBackgroundResource(R.drawable.bg_edit);
                tvAuto.setBackgroundResource(R.drawable.bg_edit);
                tvMonth.setBackgroundResource(R.drawable.bg_blackbord_press);
                break;
        }
    }

    private void initCustomTimePicker() {

        /**
         * @description
         *
         * 注意事项：
         * 1.自定义布局中，id为 optionspicker 或者 timepicker 的布局以及其子控件必须要有，否则会报空指针.
         * 具体可参考demo 里面的两个自定义layout布局。
         * 2.因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
         * setRangDate方法控制起始终止时间(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
         */
        Calendar selectedDate = Calendar.getInstance();//系统当前时间
        final Calendar startDate = Calendar.getInstance();
        startDate.set(2014, 1, 23);
        Calendar endDate = Calendar.getInstance();
        endDate.set(2027, 2, 28);
        //时间选择器 ，自定义布局
        pvCustomTime = new TimePickerView.Builder(getActivity(), new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                pvCustomTime.dismiss();
            }
        })
                .setDate(selectedDate)
                .setRangDate(startDate, endDate)
                .setLayoutRes(R.layout.pickerview_custom_time, new CustomListener() {

                    @Override
                    public void customLayout(View v) {
                        final TextView tvSubmit = (TextView) v.findViewById(R.id.tv_finish);
                        ImageView ivCancel = (ImageView) v.findViewById(R.id.iv_cancel);
                        tvSubmit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomTime.returnData();
                            }
                        });
                        ivCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomTime.dismiss();
                            }
                        });
                    }
                })
                .setType(new boolean[]{true, true, true, false, false, false})
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setDividerColor(Color.RED)
                .build();


        //时间选择器 ，自定义布局
        pvendTime = new TimePickerView.Builder(getActivity(), new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                pvendTime.dismiss();
            }
        })
                .setDate(selectedDate)
                .setRangDate(startDate, endDate)
                .setLayoutRes(R.layout.pickerview_custom_time, new CustomListener() {

                    @Override
                    public void customLayout(View v) {
                        final TextView tvSubmit = (TextView) v.findViewById(R.id.tv_finish);
                        ImageView ivCancel = (ImageView) v.findViewById(R.id.iv_cancel);
                        tvSubmit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvendTime.returnData();
                            }
                        });
                        ivCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvendTime.dismiss();
                            }
                        });
                    }
                })
                .setType(new boolean[]{true, true, true, false, false, false})
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setDividerColor(Color.RED)
                .build();

    }
    private String getTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

}
